package com.xyd.Servlet;

import com.xyd.Service.UserService;
import com.xyd.bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet({"/admin/user","/admin/userDetail","/admin/userModify","/admin/userDelete","/admin/userInsert","/UserShowDetial"})
public class AdminManageUserServlet extends HttpServlet {
    UserService userService=new UserService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getServletPath();
        if ("/admin/user".equals(path)){
            doGetAll(request,response);
        }else if ("/admin/userDetail".equals(path)){
            doDetail(request,response);
        }else if ("/admin/userModify".equals(path)){
            doModify(request,response);
        }else if ("/admin/userDelete".equals(path)){
            doDel(request,response);
        }else if("/admin/userInsert".equals(path)){
            doInsert(request,response);
        }else if ("/UserShowDetial".equals(path)){
            doUserShowDetial(request,response);
        }
    }
    //user-detial
    public void doUserShowDetial(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        int uid = Integer.parseInt(request.getParameter("uid"));
        User user = userService.getByUid(uid);
        request.getSession().setAttribute("user",user);
        request.getRequestDispatcher("/user-detial.jsp").forward(request,response);
    }
    //添加user
    private void doInsert(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String uname = request.getParameter("uname");
        String upass = request.getParameter("upass");
        String sex = request.getParameter("sex");
        String birth = request.getParameter("birthday");
        String identitycode = request.getParameter("identitycode");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        Date birthday=null;
        try {
            birthday= sdf.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        User user = new User(uname,upass,sex,birthday,identitycode,email,phone,address);
        int i = userService.addUser(user);
        if (i > 0){
            response.sendRedirect("/manage/manage-result.html");
        }
    }

    //根据uid删除uesr信息
    private void doDel(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        int i = userService.delByUid(uid);
        if (i > 0){
            response.sendRedirect("/admin/user");
        }
    }

    //根据uid更新uesr信息
    private void doModify(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        int uid = Integer.parseInt(request.getParameter("uid"));
        String uname = request.getParameter("uname");
        String upass = request.getParameter("upass");
        String sex = request.getParameter("sex");
        String birth = request.getParameter("birthday");
        String identitycode = request.getParameter("identitycode");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        Date birthday=null;
        try {
            birthday= sdf.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        User user = new User(uid,uname,upass,sex,birthday,identitycode,email,phone,address);
        int i = userService.modifyByUid(user);
        if (i > 0){
            response.sendRedirect("/manage/manage-result.html");
        }
    }

    //根据uid获取对应user的详细信息
    private void doDetail(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uid = request.getParameter("uid");
        User user = userService.getByUid(Integer.parseInt(uid));
        if (user != null){
            request.setAttribute("user",user);
            request.getRequestDispatcher("/manage/user-modify.jsp").forward(request,response);
        }
    }

    //获得所有用户信息
    private void doGetAll(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<User> users = userService.getAll();
        HttpSession session = request.getSession();
        if (users.size()>0){
            session.setAttribute("users",users);
            request.getRequestDispatcher("/manage/user.jsp").forward(request,response);
        }
    }
}
